/**
 * 
 * 修改时间：2010-06-24
 * 修改人：朱家进
 * 修改内容：添加市级社会渠道新增厅功能
 */

package com.zjht.gmcc.servlet.sys;

import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zjht.gmcc.bean.BaseServlet;
import com.zjht.gmcc.bean.util.InitParam;
import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyJDBC;
import com.zjht.gmcc.bean.util.MyParamUtil;

public class OrgaAddCitySociety extends BaseServlet {

	private static final long serialVersionUID = 1L;

	public boolean process(HttpServletRequest request, HttpServletResponse response, HttpSession session, PrintWriter out, MyJDBC myJDBC, String menu)
			throws Exception {

		String porgatype = request.getParameter("porgaType");// 父组织类型
		String porgacode = request.getParameter("porgacode");// 父组织编码

		String orgacode = request.getParameter("orgacode").trim().toUpperCase();// 新添加组织编码
		String organame = request.getParameter("organame");// 新添加组织名称

		String kind = request.getParameter("kind");// 标志位，添加省级组织 == 1 否则为添加下级组织
		String channelType = request.getParameter("channelType");// 渠道类型

		int flag = 0;
		PreparedStatement ps = null;
		ResultSet rs = null;

		String shortname = request.getParameter("shortname");
		String linkman = request.getParameter("linkman");
		String postcode = request.getParameter("postcode");
		String fax = request.getParameter("fax");
		String tel = request.getParameter("tel");
		String address = request.getParameter("address");
		String areaid = request.getParameter("areaid");
		String status = (request.getParameter("status") == null) ? "0" : request.getParameter("status");

		if (porgatype == null || porgatype.length() == 0) {
			alert(out, "无法更新组织信息，请与管理员联系。");
			return false;
		}

		String sql = null;
		String hallOrgid = String.valueOf(MyParamUtil.getSEQ("seq_orgid"));

		String halltypeid = request.getParameter("halltype");
		String fillhour = request.getParameter("fillhour");
		String sdate1 = request.getParameter("sdate1");
		String sdate2 = request.getParameter("sdate2");
		String edate1 = request.getParameter("edate1");
		String edate2 = request.getParameter("edate2");
		sql = "INSERT INTO organiza(orgid,orgacode,organame,orgatypeid,parentorgid,actorcode,areaid,address,postcode,linkman,telephone,fax,createdate,stopdate,shortname,paraid,status,appointreceiver,appointaddress,appointtel,orderid,sdate,edate,fillhour,channeltype,multiplat,parentorgacode)  "
				+ " SELECT ?,?,?,orgatypeid+1,orgid,actorcode,?,?,?,?,?,?,SYSDATE,null,? ,"
				+ "?,?,null,null,null,null,?,?,?,?,multiplat,orgacode "
				+ " FROM organiza where orgacode = ?";
		ps = myJDBC.prepareStatement(sql);
		ps.setString(1, hallOrgid);
		ps.setString(2, orgacode);
		ps.setString(3, organame);
		ps.setString(4, areaid);
		ps.setString(5, address);
		ps.setString(6, postcode);
		ps.setString(7, linkman);
		ps.setString(8, tel);
		ps.setString(9, fax);
		ps.setString(10, shortname);
		ps.setString(11, halltypeid);
		ps.setString(12, status);
		ps.setString(13, sdate1 + ":" + sdate2);
		ps.setString(14, edate1 + ":" + edate2);
		ps.setString(15, fillhour);
		ps.setString(16, channelType);
		ps.setString(17, porgacode);
		flag = ps.executeUpdate();
		ps.close();

		if (flag == 1) {
			flag = 0;
			String provider = request.getParameter("provider");
			String providerTD = request.getParameter("providerTD");// CDG2009-01-05TD手机供货商

			if (providerTD != null && providerTD.equals(provider)) {// 2G手机和TD手机是同一个供货商
				if (provider != null && provider.length() > 0 && provider.indexOf(",") != -1) {
					sql = "insert into MOBILETOPROVIDER(mobileorgacode, providerorgacode, cityproviderorgacode, status, "
							+ "ValidDate, CityProviderType) " + "VALUES(?, ?, ?, 1, Sysdate, '1')";
					ps = myJDBC.prepareStatement(sql);
					ps.setString(1, orgacode);
					ps.setString(2, provider.substring(0, provider.indexOf(",")));
					ps.setString(3, provider.substring(provider.indexOf(",") + 1, provider.length()));
					flag = ps.executeUpdate();
					ps.close();
					ps = null;
				}
			}

		}

		if (flag == 1)// 更新BOSS渠道标识
		{
			String mobileorgacode = request.getParameter("mobileorgacode");
			if (mobileorgacode != null && mobileorgacode.length() > 0) {
				sql = "INSERT INTO mobileorgatolocal(mobileorgacode,orgacode)VALUES(?,?)";
				ps = myJDBC.prepareStatement(sql);
				ps.setString(1, mobileorgacode);
				ps.setString(2, orgacode);
				flag = ps.executeUpdate();
				ps.close();
			}
		}
		if (flag == 1 && porgatype.equals("3"))// 如果添加服务厅，则需要添加服务厅库存最高限额
		{
			/*String ssprice = request.getParameter("ssprice");
			if (ssprice != null && ssprice.length() > 0) {
				sql = "INSERT INTO SS_PRICE (ID,OrgaCode,Price,CreateDate,CreateOperCode) VALUES (SS_PriceID.NEXTVAL,?,?,SYSDATE,?)";
				ps = myJDBC.prepareStatement(sql);
				ps.setString(1, orgacode);
				ps.setString(2, ssprice);
				ps.setString(3, MySession.getUsercode(session));
				flag = ps.executeUpdate();
				ps.close();
			}*/

			if (flag == 1)// 当新增服务厅时，如本市有a.multiplat = '1'平台，则把对应关系加到mobiletoprovider
			{
				sql = "SELECT a.orgacode,a.parentorgacode FROM organiza a "
						+ "WHERE a.multiplat = '1' AND a.areaid = ? AND a.orgatypeid = 6 AND a.actorcode = 'P' AND a.status = '1'";
				ps = myJDBC.prepareStatement(sql);
				ps.setString(1, areaid);
				rs = ps.executeQuery();

				sql = "INSERT INTO mobiletoprovider(mobileorgacode, providerorgacode, cityproviderorgacode, status, "
						+ "ValidDate, CityProviderType,multiplat) " + "VALUES(?, ?, ?, 1, Sysdate, '1','1')";
				ps = myJDBC.prepareStatement(sql);

				while (rs.next()) {
					ps.setString(1, orgacode);
					ps.setString(2, rs.getString("parentorgacode"));
					ps.setString(3, rs.getString("orgacode"));
					flag = ps.executeUpdate();
					if (flag == 0)
						break;
				}
				ps.close();
				ps = null;
			}
		}

		String opersDutySql = "INSERT INTO OPERSPOPEDOM (opercode,dutyid,operid) VALUES (?,?,?)";
		PreparedStatement operDutyPs = myJDBC.prepareStatement(opersDutySql);

		String opersSql = "INSERT INTO opers(opercode, opername, passwd, orgacode, deptid,createdate, status, opertype, showflag,operid,orgid) "
				+ "VALUES(?,?,?,?,?,sysdate,1,1,1,?,?)";
		PreparedStatement operPs = myJDBC.prepareStatement(opersSql);
		String jmpassword = MyConstant.INIT_PWD;
		String hallDZ = orgacode + "1";// 厅店长
		String hallCG = orgacode + "2";// 厅仓管
		String operid = String.valueOf(MyParamUtil.getSEQ("seq_operid"));
		if (flag == 1) {
			operPs.setString(1, hallDZ);
			operPs.setString(2, "店长");
			operPs.setString(3, jmpassword);
			operPs.setString(4, orgacode);
			operPs.setString(5, "40");
			operPs.setString(6, operid);
			operPs.setString(7, hallOrgid);
			flag = operPs.executeUpdate();
		}
		if (flag == 1) {
			operDutyPs.setString(1, hallDZ);
			operDutyPs.setString(2, "4000");
			operDutyPs.setString(3, operid);
			flag = operDutyPs.executeUpdate();
		}
		operid = String.valueOf(MyParamUtil.getSEQ("seq_operid"));
		if (flag == 1) {
			operPs.setString(1, hallCG);
			operPs.setString(2, "厅仓管");
			operPs.setString(3, jmpassword);
			operPs.setString(4, orgacode);
			operPs.setString(5, "42");
			operPs.setString(6, operid);
			operPs.setString(7, hallOrgid);
			flag = operPs.executeUpdate();
		}
		if (flag == 1) {
			operDutyPs.setString(1, hallCG);
			operDutyPs.setString(2, "4200");
			operDutyPs.setString(3, operid);
			flag = operDutyPs.executeUpdate();
		}

		String act = organame + " 添加";
		if (flag > 0) {
			InitParam.initOrgaInfo();
			if (kind != null)// 添加省级组织
			{
				alertOK(out, act + "成功！", "dialogArguments.parent.orgaleftFrame.location.reload();window.close();");
			} else {
				alertOK(out, act + "成功！", "dialogArguments.location.href='orga/orga.jsp?orgacode=" + orgacode + "&channelType=" + channelType
						+ "&orgaType=" + (Integer.parseInt(porgatype) + 1) + "';window.returnValue='" + orgacode + "';window.close();");
			}

		} else {
			errAlert(request, out, menu, act + "失败");
		}
		return flag > 0;

	}
}
